package com.zl.smartmall.library;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.listener.AddHouseListener;
import com.zl.smartmall.library.listener.ApplyHouseListenser;
import com.zl.smartmall.library.listener.DeleteHouseListener;
import com.zl.smartmall.library.listener.GZCommunityInfoListener;
import com.zl.smartmall.library.listener.GetBuildingAddressListener;
import com.zl.smartmall.library.listener.GetCommunityAddressListener;
import com.zl.smartmall.library.listener.GetDictCityListener;
import com.zl.smartmall.library.listener.GetDictDistrictListener;
import com.zl.smartmall.library.listener.GetDictProvinceListener;
import com.zl.smartmall.library.listener.GetFloorAddressListener;
import com.zl.smartmall.library.listener.GetGroupByCmidAddressListener;
import com.zl.smartmall.library.listener.GetGroupsAddressListener;
import com.zl.smartmall.library.listener.GetHousesAddressListener;
import com.zl.smartmall.library.listener.GetMyCommunityAddressListener;
import com.zl.smartmall.library.listener.RequestCode;
import com.zl.smartmall.library.listener.SelectDefaultCommunityListener;
import com.zl.smartmall.library.po.GZCommunityInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.PreferenceModel;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;
import com.zl.smartmall.library.vo.GZCommunityInfoDAO;

import android.R.integer;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class IGZAddress {
	private static final int MSG_LOAD_GZ_COMMUNITY_FINISH = 1;
	private static IGZAddress mInstance;
	private static Lock mLock;
	private Context mContext;
	private IGZAddressHandler mHandler;

	static {
		mLock = new ReentrantLock();
	}

	private IGZAddress(Context context) {
		mContext = context;
		mHandler = new IGZAddressHandler(Looper.myLooper());
	}

	public static IGZAddress getInstance() {
		return mInstance;
	}

	public static void init(Context context) {
		if (mInstance == null) {
			mLock.lock();
			try {
				if (mInstance == null) {
					mInstance = new IGZAddress(context);
				}
			} finally {
				mLock.unlock();
			}
		}
	}

	public void getCommunityAddress(GetCommunityAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_COMMUNITY_ADDRESS, listener);

	}

	public void getFloorAddress(int cmid, GetFloorAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_FLOOR_ADDRESS + "?cmid=" + cmid, listener);
	}

	public void applyHouse(int cmid, int id,String phone, ApplyHouseListenser listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_APPLY_HOUSE + "?cmid=" + cmid + "&id="
						+ id+"&phone="+phone, listener);
		Log.i("duzhijuan",Constants.URL_APPLY_HOUSE + "?cmid=" + cmid + "&id="
				+ id+"&phone="+phone);
	}
	public void addHouse(int cmid, int id,String code, AddHouseListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance()
		.get(Constants.URL_ADD_HOUSE + "?cmid=" + cmid + "&id="
				+ id+"&smscode="+code, listener);
	}
	
	public void getMyCommunity(GetMyCommunityAddressListener listener){
		listener.setRequestCode(RequestCode.FIRST);
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance()
		.get(Constants.URL_GET_MY_COMMUNITY, listener);
	}
	public void refreshMyCommunity(GetMyCommunityAddressListener listener){
		listener.setRequestCode(RequestCode.PULL);
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance()
		.get(Constants.URL_GET_MY_COMMUNITY, listener);
	}
	
	/**
	 * 选择默认的小区
	 * @param cmid 小区id
	 * @param id
	 * @param listener
	 */
	public void selectDefaultCommunity(int cmid,int id,SelectDefaultCommunityListener listener){
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		listener.setCmid(cmid);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance()
		.get(Constants.URL_SELECT_DEFAULT_COMMUNITY+"?cmid="+cmid+"&id="+id, listener);
		
		CrashHandler.d("duzhijuan", "url = " + Constants.URL_SELECT_DEFAULT_COMMUNITY+"?cmid="+cmid+"&id="+id);
	}
	
	/**
	 * 判断当前用户是否已绑定小区
	 * @return 返回true表示已经绑定, 否则false
	 */
	public boolean hasCommunity() {
		return PreferenceModel.getBoolean(Constants.PREFERENCE_KEY_HAS_COMMUNITY, false);
	}
	
	/**
	 * 获取当前用户选择的默认小区的id
	 * @return
	 */
	public int getCurrentCommunityID(){
		return PreferenceModel.getInt(Constants.PREFERENCE_KEY_COMMUNITY_ID, 0);
	}
	/**
	 * 获取当前用户选择的默认房间的id
	 * @return
	 */
	public int getCurrentHouseid(){
		return PreferenceModel.getInt(Constants.PREFERENCE_KEY_HOUSEID, 0);
	}
	
	
	public String getCurrentCommunityName(){
		return PreferenceModel.getString(Constants.PREFERENCE_KEY_COMMUNITY_NAME, "双明汇");
	}
	
	public void setIDAndName(int id,String name){
		PreferenceModel.putBoolean(Constants.PREFERENCE_KEY_HAS_COMMUNITY, true);
		PreferenceModel.putInt(Constants.PREFERENCE_KEY_COMMUNITY_ID, id);
		PreferenceModel.putString(Constants.PREFERENCE_KEY_COMMUNITY_NAME, name);
	}
	public void deleteSelected(){
		PreferenceModel.putBoolean(Constants.PREFERENCE_KEY_HAS_COMMUNITY, false);
		PreferenceModel.remove(Constants.PREFERENCE_KEY_COMMUNITY_NAME);
		PreferenceModel.remove(Constants.PREFERENCE_KEY_COMMUNITY_ID);
	}
	
	/**
	 * 获取绑定的小区列表从缓存数据库
	 * @param gzCommunityInfoListener
	 */
	public void loadCommunities(final GZCommunityInfoListener gzCommunityInfoListener) {
		ThreadExecutor.getInstance().execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				MessageData msgData = new MessageData();
				msgData.listener = gzCommunityInfoListener;
				msgData.value = GZCommunityInfoDAO.getAll(mContext);
				Message msg = Message.obtain(mHandler, MSG_LOAD_GZ_COMMUNITY_FINISH, msgData);
				msg.sendToTarget();
			}
		});
	}
	/**
     * Avoid leaks by using a non-anonymous handler class.
     */
	private static class IGZAddressHandler extends Handler {
		
		public IGZAddressHandler(Looper looper) {
			super(looper);
		}
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			MessageData msgData;
			switch (msg.what) {
			case MSG_LOAD_GZ_COMMUNITY_FINISH:
				msgData = (MessageData) msg.obj;
				msgData.listener.onResponseSuccess(msgData.value);
				break;
				
			default:
				break;
			}
		}
	}
	
	private static class MessageData { 
		GZCommunityInfoListener listener;
		List<GZCommunityInfo> value;
	}
	
	
	public void deleteHouse(int cmid,int id,DeleteHouseListener listener){
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance()
		.get(Constants.URL_DELETE_COMMUNITY+"?cmid="+cmid+"&id="+id, listener);
	}
	/**
	 * 根据小区的id回去相应的组团的信息
	 * @param cmid
	 * @param listener
	 */
	public void getGroups(String district_id,int page, GetGroupsAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		RequestParams params = new RequestParams();
		params.put("page", page);
		params.put("district_id", district_id);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_COMMUNITYLIST,params, listener);
	}
	public void getGroupBycmid(int cmid, GetGroupByCmidAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		RequestParams params = new RequestParams();
		params.put("cmid", cmid);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_TEAMS,params, listener);
	}
	/**
	 * 根据组团的id回去相应的楼的信息
	 * @param cmid
	 * @param listener
	 */
	public void getBuilding(int groupid, GetBuildingAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_BUILDING + "?groupid=" + groupid, listener);
		Log.i("Happy",Constants.URL_GET_BUILDING + "?groupid=" + groupid);
	}
	/**
	 * 根据楼的id回去相应的房屋的信息
	 * @param cmid
	 * @param listener
	 */
	public void getHouses(int building_id, GetHousesAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_HOUSE + "?building_id=" + building_id, listener);
	}
	/**
	 * 查询省
	 * @param listener
	 */
	public void getDictProvince(GetDictProvinceListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_DICTPROVINCE, listener);
	}
	/**
	 * 查询市级
	 * @param listener
	 */
	public void getDictCity(String province_id,GetDictCityListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		RequestParams params = new RequestParams();
		params.put("province_id", province_id);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_DICTCITY,params, listener);
	}
	/**
	 * 查询县级
	 * @param listener
	 */
	public void getDictDistrict(String city_id,GetDictDistrictListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		RequestParams params = new RequestParams();
		params.put("city_id", city_id);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_DICTDISTRICT,params, listener);
	}
	/*public void getCommunity_list(String district_id,GetCommunityAddressListener listener) {
		if (!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
			return;
		}
		RequestParams params = new RequestParams();
		params.put("dict_district_id", district_id);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_COMMUNITY_LIST,params ,listener);
	}*/
}
